package com.im.service.user.controller;

import com.im.common.model.RestResponse;
import com.im.common.route.RouteInfo;
import com.im.service.user.model.param.*;
import com.im.service.user.model.result.ImportUserResult;
import com.im.service.user.service.ImUserService;
import com.im.service.user.service.ImUserStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private ImUserService imUserService;
    @Autowired
    private ImUserStatusService imUserStatusService;

    @ApiOperation("批量导入用户")
    @RequestMapping(value = "/importUser", method = RequestMethod.POST)
    public RestResponse importUser(@RequestBody ImportUserParam param, Integer appId) {
        param.setAppId(appId);
        ImportUserResult result = imUserService.importUser(param);
        return RestResponse.success(result);
    }

    @ApiOperation("批量删除用户")
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public RestResponse deleteUser(@RequestBody @Validated DeleteUserParam param, Integer appId) {
        param.setAppId(appId);
        ImportUserResult result = imUserService.deleteUser(param);
        return RestResponse.success(result);
    }

    @ApiOperation(value = "im的登录接口，返回im地址", notes = "根据登录参数和应用ID进行登录验证，并根据客户端类型选择合适的服务器进行路由。")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public RestResponse login(@RequestBody @Validated LoginParam param, Integer appId) {
        param.setAppId(appId);
        RouteInfo login = imUserService.login(param);
        return RestResponse.success(login);
    }

    @ApiOperation(value = "获取用户序列号", notes = "用于客户端判断是否需要进行数据同步")
    @PostMapping("/getUserSequence")
    public RestResponse getUserSequence(@RequestBody @Validated GetUserSequenceParam req, @RequestParam("appId") Integer appId) {
        req.setAppId(appId);
        Map<String, Object> map = imUserService.getUserSequence(req);
        return RestResponse.success(map);
    }

    @ApiOperation("订阅用户在线状态")
    @PostMapping("/subscribeUserOnlineStatus")
    public RestResponse subscribeUserOnlineStatus(@RequestBody @Validated SubscribeUserOnlineStatusReq req, @RequestParam("appId") Integer appId, @RequestParam("identifier") String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        imUserStatusService.subscribeUserOnlineStatus(req);
        return RestResponse.success();
    }

    @ApiOperation("设置用户自定义状态")
    @PostMapping("/setUserCustomerStatus")
    public RestResponse setUserCustomerStatus(@RequestBody @Validated SetUserCustomerStatusReq req, @RequestParam("appId") Integer appId, @RequestParam("identifier") String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        imUserStatusService.setUserCustomerStatus(req);
        return RestResponse.success();
    }

    @ApiOperation("查询好友在线状态")
    @PostMapping("/queryFriendOnlineStatus")
    public RestResponse queryFriendOnlineStatus(@RequestBody @Validated PullFriendOnlineStatusReq req, @RequestParam("appId") Integer appId, @RequestParam("identifier") String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        return RestResponse.success(imUserStatusService.queryFriendOnlineStatus(req));
    }

    @ApiOperation("查询用户在线状态")
    @PostMapping("/queryUserOnlineStatus")
    public RestResponse queryUserOnlineStatus(@RequestBody @Validated PullUserOnlineStatusReq req, @RequestParam("appId") Integer appId, @RequestParam("identifier") String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        return RestResponse.success(imUserStatusService.queryUserOnlineStatus(req));
    }
}
